package com.administrator.customviewtest.view_group;

import android.content.Context;
import android.support.v4.view.ViewConfigurationCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.Scroller;

/**
 * Created by Administrator on 2017/2/5.
 */

public class TestViewGrop5 extends ViewGroup {
    //滑动的临界值
    private int touchSlop;
    //左边界
    private int leftBorder;
    //右边界
    private int rightBorder;

    private float xDown,xMove,xLastDown;
    /**
     * 用于完成滚动操作的实例
     */
    private Scroller mScroller;
    public TestViewGrop5(Context context) {
        this(context, null);
    }

    public TestViewGrop5(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TestViewGrop5(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init(context);
    }

    private void init(Context context) {
        mScroller = new Scroller(context);
        ViewConfiguration configuration = ViewConfiguration.get(context);
        // 获取TouchSlop值
        touchSlop = ViewConfigurationCompat.getScaledPagingTouchSlop(configuration);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildren(widthMeasureSpec,heightMeasureSpec);
    }


    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        View mainView = getChildAt(0);
        mainView.layout(0,0,getMeasuredWidth(),mainView.getMeasuredHeight());
        View menuView = getChildAt(1);
        menuView.layout(getMeasuredWidth(),0,getMeasuredWidth()+menuView.getMeasuredWidth(),menuView.getMeasuredHeight());

        leftBorder = mainView.getLeft();
        rightBorder = menuView.getRight();
    }
//
//


//    @Override
//    public boolean onInterceptTouchEvent(MotionEvent ev) {
//        switch (ev.getAction()){
//            case MotionEvent.ACTION_DOWN:
//                xDown = ev.getRawX();
//                xLastDown = xDown;
//                break;
//            case MotionEvent.ACTION_MOVE:
//                xMove = ev.getRawX();
//                float diff = Math.abs(xMove - xDown);
//                xLastDown = xMove;
//                // 当手指拖动值大于TouchSlop值时，认为应该进行滚动，拦截子控件的事件
//                if (diff > touchSlop) {
//                    return true;
//                }
//                break;
//        }
//        return super.onInterceptTouchEvent(ev);
//    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                xDown = ev.getRawX();
                xLastDown = xDown;
                break;
            case MotionEvent.ACTION_MOVE:
                xMove = ev.getRawX()-xDown;
                xLastDown = ev.getRawX();
                if(Math.abs(xMove)>touchSlop){
                    return true;
                }
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }

//        @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        switch (event.getAction()){
//            case MotionEvent.ACTION_MOVE:
//                xMove = event.getRawX();
//                int scrolledX = (int) (xLastDown - xMove);
//                if (getScrollX() + scrolledX < leftBorder) {
//                    scrollTo(leftBorder, 0);
//                    return true;
//                } else if (getScrollX() + getWidth() + scrolledX > rightBorder) {
//                    scrollTo(rightBorder - getWidth(), 0);
//                    return true;
//                }
//                scrollBy(scrolledX, 0);
//                xLastDown = xMove;
//                break;
//        }
//        return super.onTouchEvent(event);
//    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_MOVE:
                xMove = xLastDown-event.getRawX();
                if(getScrollX()+xMove<leftBorder){
                    scrollTo(leftBorder,0);
                    return true;
                }else if(getScrollX()+getWidth()+xMove>rightBorder){
                    scrollTo(rightBorder-getWidth(),0);
                    return true;
                }
                    scrollBy((int) xMove,0);
                xLastDown = event.getRawX();
                break;
            case MotionEvent.ACTION_UP:
                // 当手指抬起时，根据当前的滚动值来判定应该滚动到哪个子控件的界面
                int targetIndex = (getScrollX() + getWidth() / 2) / getWidth();
                int dx = targetIndex * getWidth() - getScrollX();
                // 第二步，调用startScroll()方法来初始化滚动数据并刷新界面
                mScroller.startScroll(getScrollX(), 0, dx, 0);
                invalidate();
                break;
        }
        return super.onTouchEvent(event);
    }
}
